﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace DARE
{
    public class CAttach
    {
        #region Fields

        private static int id = 0;
        
        private int m_id;

        private int m_index;
        private CNode m_hNode;
        private CNode m_node;
        private CNode m_oldNode;

        #endregion

        #region Property

        //internal bool IsAttachToBone { get; set; }

        public Quaternion RelativeRotation
        {
            get { return m_node.RelativeOrientation; }
            set
            {
                if (value != m_node.RelativeOrientation)
                    m_node.SetOrientation(value, CNode.ESpace.PARENT);
            }
        }

        public Vector3 RelativePosition
        {
            get { return this.m_node.RelativePosition; }
            set
            {
                if (value != this.m_node.RelativePosition)
                    m_node.SetPosition(value, CNode.ESpace.PARENT);
            }
        }

        public Vector3 RelativeScale
        {
            get { return this.m_node.RelativeScale; }
            set
            {
                if (value != this.m_node.RelativeScale)
                    m_node.SetScale(value);
            }
        }

        public bool IsAttach { get; private set; }

        public CNode Node
        {
            get { return this.m_node; }
        }

        #endregion

        #region Event

        public delegate void AttachEvent(CAttach _attach, CNode _oldParentNode);

        public event AttachEvent Detached;

        #endregion

        #region CTOR

        internal CAttach(int index, CNode node)
        {
            Reset(index, node);
        }

        #endregion

        #region Method

        public void Reset(int index, CNode node)
        {
            m_id = id++;

            m_oldNode = node.Parent;
            node.Detach();
            m_node = node;
            
            m_node.Smooth = false;

            m_hNode = node.Render3D.CreateNode(EType.STATIC, true);
            m_hNode.AttachChild(node);

            m_index = index;

//            IsAttachToBone = _isAttachToBone;
            IsAttach = true;
        }

        public void Update(CAnimationController _anim)
        {
            Matrix _root = _anim.WorldTransform[m_index];

            Vector3 _pos = Vector3.Zero;
            Vector3 _scale = Vector3.Zero;
            Quaternion _rot = Quaternion.Identity;

            bool result = _root.Decompose(out _scale, out _rot, out _pos);
            Debug.Assert(result);

            m_hNode.SetScale(_scale);
            m_hNode.SetOrientation(_rot, CNode.ESpace.WORLD);
            m_hNode.SetPosition(_pos, CNode.ESpace.WORLD);
        }

        public CNode Detach()
        {
            IsAttach = false;
            m_node.Detach();

            m_hNode.Render3D.RemoveNode(m_hNode);
            if (Detached != null)
                Detached(this, m_oldNode);
            return m_node;
        }

        #endregion
    }
}
